﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_KetQuaTimKiemTheoNguoiDung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            //Kiem tra co dang nhap khong?
            if (Session["MaNguoiDung"] == null || Session["MaNguoiDung"].ToString() == string.Empty)
            {
                Response.Redirect("~/pHome.aspx");
            }

            //
            layDanhSachPhongNhanVanBan();

            //Phan quyen de tim kiem van ban da gui cho nguoi dung nao
            string kt = kiemtraMaLoaiNguoiDung();
            switch (kt)
            {
                case "KSCN":
                    drplstMaPhongNhanVanBan.Enabled = true;
                    btnChuyenVanBan.Visible = true;
                    break;

                case "KSPT":
                    PhongBUS pBUS = new PhongBUS();
                    string maPhong = pBUS.layMaPhongTheoMaNguoiDung(Session["MaNguoiDung"].ToString());
                    pBUS.sapxepPhong(drplstMaPhongNhanVanBan, maPhong);
                    layDanhSachNguoiDungTheoMaPhong();
                    //drplstMaPhongNhanVanBan.Enabled = true;
                    btnChuyenVanBan.Visible = true;
                    break;

                case "USER":
                    layTenNguoiDungTheoMaNguoiDung();
                    break;

                case "ADMIN":
                    layTenNguoiDungTheoMaNguoiDung();
                    break;
            }
        }
    }

    protected void layDanhSachPhongNhanVanBan()
    {
        PhongBUS pBUS = new PhongBUS();

        pBUS.themItemVaoDropDownList(drplstMaPhongNhanVanBan);
    }
    protected void layDanhSachNguoiDungTheoMaPhong()
    {
        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string maPhong = drplstMaPhongNhanVanBan.SelectedValue.ToString();

        gvNoiNhanVanBan.DataSource = ndBUS.layDanhSachNguoiDungTheoMaPhong(maPhong);
        gvNoiNhanVanBan.DataBind();
    }
    protected void drplstPhongNhanVanBan_SelectedIndexChanged(object sender, EventArgs e)
    {
        layDanhSachNguoiDungTheoMaPhong();

        if (gvNoiNhanVanBan.Rows.Count > 0)
        {
            chkChonTatCa.Visible = true;
        }
        else
        {
            chkChonTatCa.Visible = false;
        }

        if (chkChonTatCa.Checked)
        {
            chkChonTatCa.Checked = false;
        }
    }
    protected void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
    {
        MyCheckBox myCheckBox = new MyCheckBox();

        if (chkChonTatCa.Checked)
        {
            myCheckBox.chonTatCa(gvNoiNhanVanBan, "chkMaNguoiDung", chkChonTatCa);
        }
        else
        {
            myCheckBox.huyTatCa(gvNoiNhanVanBan, "chkMaNguoiDung", chkChonTatCa);
        }
    }
    protected void timkiemVanBanTheoNguoiDung()
    {
        /*VanBan vbDTO = new VanBan();
        vbDTO.SoVanBan = (String)Session["SoVanBan"];
        vbDTO.TenVanBan = (String)Session["TenVanBan"];
        string _ngayTu = (String)Session["NgayTu"];
        string _ngayDen = (String)Session["NgayDen"];

        string maNguoiDung = (String)Session["MaNguoiDung"];

        if (vbDTO.SoVanBan != string.Empty && _ngayTu == string.Empty && _ngayDen == string.Empty)
        {
            DateTime ngayTu = new DateTime(1753, 01, 01);

            DateTime ngayDen = new DateTime(9999, 12, 31);

            VanBanBUS vbBUS = new VanBanBUS();
            gvKetQuaTimKiem.DataSource = vbBUS.timkiemDanhSachVanBanTheoMaNguoiDung(maNguoiDung,vbDTO, ngayTu, ngayDen);
            gvKetQuaTimKiem.DataBind();
        }

        if (vbDTO.TenVanBan != string.Empty && _ngayTu == string.Empty && _ngayDen == string.Empty)
        {
            DateTime ngayTu = new DateTime(1753, 01, 01);

            DateTime ngayDen = new DateTime(9999, 12, 31);

            VanBanBUS vbBUS = new VanBanBUS();
            gvKetQuaTimKiem.DataSource = vbBUS.timkiemDanhSachVanBanTheoMaNguoiDung(maNguoiDung, vbDTO, ngayTu, ngayDen);
            gvKetQuaTimKiem.DataBind();
        }

        if (_ngayTu != string.Empty && _ngayDen != string.Empty)
        {
            DateTime ngayTu = Convert.ToDateTime(_ngayTu);

            DateTime ngayDen = Convert.ToDateTime(_ngayDen);

            VanBanBUS vbBUS = new VanBanBUS();
            gvKetQuaTimKiem.DataSource = vbBUS.timkiemDanhSachVanBanTheoMaNguoiDung(maNguoiDung, vbDTO, ngayTu, ngayDen);
            gvKetQuaTimKiem.DataBind();
        }*/


        for (int i = 0; i < gvNoiNhanVanBan.Rows.Count; i++)
        {
            CheckBox _checkBox = (CheckBox)(gvNoiNhanVanBan.Rows[i].Cells[0].FindControl("chkMaNguoiDung"));
            if (_checkBox.Checked == true)
            {
                string maNguoiDung = gvNoiNhanVanBan.DataKeys[i].Value.ToString();
                VanBanBUS vbBUS = new VanBanBUS();
                gvKetQuaTimKiem.DataSource = vbBUS.timkiemDanhSachVanBanTheoMaNguoiDung(maNguoiDung);
                gvKetQuaTimKiem.DataBind();
            }
        }
    }
    protected void btnDongY_Click(object sender, EventArgs e)
    {
        timkiemVanBanTheoNguoiDung();
    }
    protected void btnDong_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/pHome.aspx");
    }
    protected string themSo0(int soLuong)
    {
        string temp = string.Empty;
        for (int i = 0; i < soLuong; i++)
        {
            temp += "0";
        }

        return temp;
    }
    protected string kiemtraMaLoaiNguoiDung()
    {
        LoaiNguoiDungBUS lndBUS = new LoaiNguoiDungBUS();

        string maNguoiDung = (String)Session["MaNguoiDung"];

        string maLoaiNguoiDung = lndBUS.layMaLoaiNguoiDungTheoMaNguoiDung(maNguoiDung);

        return maLoaiNguoiDung;

        /*switch (maLoaiNguoiDung)
        {
            case "ADMIN":
                Response.Redirect("~/QLVB/Admin/aHome.aspx");
                break;

            case "USER":
                Response.Redirect("SoanThao.aspx");
                break;

            case "KSCN":
                Response.Redirect("DuyetSoanThao.aspx");
                break;

            case "KSPT":
                Response.Redirect("DuyetSoanThao.aspx");
                break;
        }*/
    }
    protected void layTenNguoiDungTheoMaNguoiDung()
    {
        NguoiDungBUS ndBUS = new NguoiDungBUS();

        string maNguoiDung = (String)Session["MaNguoiDung"];

        gvNoiNhanVanBan.DataSource = ndBUS.layTenNguoiDungTheoMaNguoiDung(maNguoiDung);
        gvNoiNhanVanBan.DataBind();
    }
    protected void btnChuyenVanBan_Click(object sender, EventArgs e)
    {
        MyCheckBox myCheckBox = new MyCheckBox();
        bool kiemtra1 = myCheckBox.kiemtraCheck(gvKetQuaTimKiem, "chkMaVanBan");
        if (kiemtra1 == true)
        {
            bool kiemtra2 = myCheckBox.kiemtraCheck(gvNoiNhanVanBan, "chkMaNguoiDung");
            if (kiemtra2 == true)
            {
                NguoiDungBUS ndBUS = new NguoiDungBUS();
                ndBUS.themMaVanBanTheoMaNguoiDung(gvKetQuaTimKiem, gvNoiNhanVanBan);
                Alert.Show("Da chuyen van ban thanh cong");
            }
            else
            {
                Alert.Show("Chua chon can bo de chuyen van ban");
                return;
            }
        }
        else
        {
            Alert.Show("Chua chon van ban de chuyen");
            return;
        }
    }
    protected void gvKetQuaTimKiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvKetQuaTimKiem.PageIndex = e.NewPageIndex;

        timkiemVanBanTheoNguoiDung();

        chkChonTatCa_.Checked = false;
    }
    protected void gvNoiNhanVanBan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNoiNhanVanBan.PageIndex = e.NewPageIndex;

        layDanhSachNguoiDungTheoMaPhong();
    }
    protected void gvKetQuaTimKiem_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        /*Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;
            if (e.Row.DataItemIndex >= 0)
            {
                maVanBan = gvKetQuaTimKiem.DataKeys[e.Row.DataItemIndex].Value.ToString();
            }
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBan(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;
         }*/

        Label lblNoiNhan = (Label)(e.Row.FindControl("lblNoiNhan"));
        if (lblNoiNhan != null)
        {
            string sNoiNhan = string.Empty;
            string maVanBan = string.Empty;
            maVanBan = gvKetQuaTimKiem.DataKeys[e.Row.RowIndex].Value.ToString();
            List<NoiNhan> lstNoiNhan = (new NoiNhanBUS()).layDanhSachMaNguoiNhanTheoMaVanBanDaDuyet(maVanBan);
            foreach (NoiNhan nn in lstNoiNhan)
            {
                sNoiNhan += nn.MaNguoiNhan.ToString() + "\n"; //Xuong dong
                //sNoiNhan += nn.MaNguoiNhan.ToString() + ",";    //Them dau phay ","
            }
            lblNoiNhan.Text = sNoiNhan;
        }
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        if (txtSoVanBanCanTim.Text == string.Empty)
        {
            for (int i = 0; i < gvNoiNhanVanBan.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gvNoiNhanVanBan.Rows[i].Cells[0].FindControl("chkMaNguoiDung"));
                if (_checkBox.Checked == true)
                {
                    string maNguoiDung = gvNoiNhanVanBan.DataKeys[i].Value.ToString();
                    VanBanBUS vbBUS = new VanBanBUS();
                    gvKetQuaTimKiem.DataSource = vbBUS.timkiemDanhSachVanBanTheoMaNguoiDung(maNguoiDung);
                    gvKetQuaTimKiem.DataBind();
                }
            }
        }
        else
        {
            for (int i = 0; i < gvNoiNhanVanBan.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gvNoiNhanVanBan.Rows[i].Cells[0].FindControl("chkMaNguoiDung"));
                if (_checkBox.Checked == true)
                {
                    string soVanBan = txtSoVanBanCanTim.Text.ToUpper();
                    string maNguoiDung = gvNoiNhanVanBan.DataKeys[i].Value.ToString();
                    VanBanBUS vbBUS = new VanBanBUS();
                    gvKetQuaTimKiem.DataSource = vbBUS.timkiemSoVanBanDaNhanTheoMaNguoiDung(soVanBan, maNguoiDung);
                    gvKetQuaTimKiem.DataBind();
                }
            }
        }
    }
    protected void chkChonTatCa__CheckedChanged(object sender, EventArgs e)
    {
        MyCheckBox myCheckBox = new MyCheckBox();

        if (chkChonTatCa_.Checked)
        {
            myCheckBox.chonTatCa(gvKetQuaTimKiem, "chkMaVanBan", chkChonTatCa_);
        }
        else
        {
            myCheckBox.huyTatCa(gvKetQuaTimKiem, "chkMaVanBan", chkChonTatCa_);
        }
    }
    //protected void capnhatNgayXemVanBan()
    //{
    //    NoiNhan nnDTO = new NoiNhan();
    //    nnDTO.MaNguoiNhan = (String)Session["MaNguoiDung"];
    //    nnDTO.MaVanBan = 
    //}
}